package com.brp.util;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.nio.charset.Charset;
import java.util.List;

/**
 * @Auther: dy
 * @Date: 2022/3/16 - 03 - 16 - 10:28
 * @Description: com.dy.util
 * @version: 1.0
 */
@Configuration //全局配置
@ComponentScan(basePackages = {"com.brp.controller"}) // 注解扫描器
@EnableWebMvc // springmvc配置
public class WebConfig implements WebMvcConfigurer {
    //过滤静态资源
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //根目录下的任何静态资源被请求时，都到/static/目录下去找静态资源  这里是配置的classpath类路径
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    }
    //配置视图解析器前后缀
    @Bean
    public ViewResolver initViewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }
    //消息转换器，防止出现乱码的情况 --> spring自带的过滤器filter
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        StringHttpMessageConverter stringHttpMessageConverter = (StringHttpMessageConverter) converters.get(1);
        stringHttpMessageConverter.setDefaultCharset(Charset.forName("utf-8"));
    }
}
